package cn.moningxi.controller;

import cn.moningxi.bean.*;
import cn.moningxi.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
public class GoodsInfomationController {
    @Autowired
    GoodsInfomationService goodsInfomationService;
    @Autowired
    GGoodsBrandService gGoodsBrandService;
    @Autowired
    GoodsCategoryService goodsCategoryService;
    @Autowired
    CCollectService cCollectService;
    @Autowired
    GoodsBusinessService goodsBusinessService;

    /**
     * 商品搜索
     *
     * @param mv
     * @param keywords
     * @param categoryId
     * @return
     */
    @RequestMapping("goodsIndexApi/queryGoodsByParam/{page}")
    @ResponseBody
    public ModelAndView queryGoodsByParam(ModelAndView mv, @PathVariable("page") Integer page, String keywords, Integer categoryId) {
        PageInfo<GoodsInfomation> goodsInfomationList = new PageInfo<>();
        List<GGoodsBrand> gGoodsBrandList = new ArrayList<>();
        List<GoodsCategory> categoryList = goodsCategoryService.getCateList();
        //搜索条件
        if (keywords != null) {
            PageHelper.startPage(page, 5);
            goodsInfomationList = new PageInfo<>(goodsInfomationService.queryGoodsByName(keywords), 3);
            Set<String> categoryIds = new HashSet<>();
            gGoodsBrandList = new ArrayList<>();
            if (goodsInfomationList.getList().size() != 0) {
                for (GoodsInfomation goodsInfomation : goodsInfomationList.getList()) {
                    categoryIds.add(goodsInfomation.getCatyId());
                }
                gGoodsBrandList = gGoodsBrandService.getListByListCId(categoryIds);
            }
            mv.addObject("keywords", keywords);
        } else if (categoryId != null) {
            //分类栏点击
            PageHelper.startPage(page, 5);
            goodsInfomationList = new PageInfo<>(goodsInfomationService.queryGoodsByCategoryId(categoryId), 3);
            gGoodsBrandList = gGoodsBrandService.getListByCId(categoryId);
            mv.addObject("categoryId", categoryId);
        }
        mv.addObject("goodsList", goodsInfomationList);
        mv.addObject("brandList", gGoodsBrandList);
        mv.addObject("categoryList", categoryList);
        mv.setViewName("business/home/search");
        List<GoodsCategory> goodsCategoryList = goodsCategoryService.getHotList();
        mv.addObject("hotSaleList", goodsCategoryList);
        return mv;
    }

    /**
     * 查看商品详细信息
     *
     * @param mv
     * @param goodsId
     * @return
     */
    @RequestMapping("goodsIndexApi/queryGoodsDetail")
    @ResponseBody
    public ModelAndView queryGoodsDetail(ModelAndView mv, String goodsId, HttpServletRequest request) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        GoodsInfomation goodsInfomation = goodsInfomationService.queryGoodsById(goodsId);
        CCollect cCollect = cCollectService.queryByCustomerIdAndGoodId(onlineUser.getCustomerId(), goodsId);
        mv.addObject("collectVo", cCollect);
        mv.addObject("goodsInfo", goodsInfomation);
        mv.setViewName("business/home/introduction");
        return mv;
    }

    /**
     * 查询全部商品信息
     *
     * @param modelAndView
     * @return
     */
    @RequestMapping("/allGoodsInfo")
    public ModelAndView selectAllGoodsInfo(ModelAndView modelAndView) {
        List<GoodsInfomation> goods = goodsInfomationService.selectAll();
        modelAndView.addObject("goods", goods);
        modelAndView.setViewName("backstage/product-list");
        return modelAndView;
    }

    /**
     * 根据商品名称关键字模糊查询商品
     *
     * @param modelAndView
     * @param nameInfo
     * @return
     */
    @RequestMapping("/allGoodsInfo/seacherGoodsByName")
    @ResponseBody
    public ModelAndView seacherGoodsByName(ModelAndView modelAndView, String nameInfo) {
        System.out.println("前台传过来的关键词是：" + nameInfo);
        List<GoodsInfomation> goods = goodsInfomationService.seacherGoodsByName(nameInfo);
        modelAndView.addObject("nameInfo", nameInfo);
        modelAndView.addObject("goods", goods);
        modelAndView.setViewName("backstage/product-list");
        return modelAndView;
    }

    /**
     * 跳转到修改商品信息界面
     *
     * @param modelAndView
     * @param goodsId
     * @return
     */
    @RequestMapping("/editGoodsInfo/{goodsId}")
    public ModelAndView editGoodsInfo(ModelAndView modelAndView, @PathVariable("goodsId") String goodsId) {
        GoodsInfomation goods = goodsInfomationService.queryGoodsById(goodsId);
        modelAndView.addObject("goods", goods);
        modelAndView.setViewName("backstage/producet-edit");
        return modelAndView;
    }

    /**
     * 将修改后的商品信息更新到数据库中
     *
     * @param goodsInfomation
     * @param modelAndView
     * @return
     */
    @RequestMapping("/updateGoodsInfo")
    public ModelAndView updateGoodsInfo(GoodsInfomation goodsInfomation, ModelAndView modelAndView) {
        System.out.println(goodsInfomation);
        goodsInfomationService.updateGoodsInfo(goodsInfomation);
        modelAndView.setViewName("redirect:/allGoodsInfo");
        return modelAndView;
    }

    /**
     * 添加商品
     *
     * @param modelAndView
     * @param goodsInfomation
     * @return
     */
    @RequestMapping(value = "/addGoods", method = RequestMethod.POST)
    public ModelAndView addGoods(ModelAndView modelAndView, GoodsInfomation goodsInfomation) {
        System.out.println("添加的商品信息：" + goodsInfomation);
        //校验商品货号是否已存在（即添加的商品是否已存在）
        GoodsInfomation Goods = goodsInfomationService.queryByGoodaSn(goodsInfomation.getGoodsSn());
        //校验店铺编码是否正确
        GoodsBusiness business = goodsBusinessService.queryBySn(goodsInfomation.getBusinessSn());
        if (Goods != null) {
            modelAndView.addObject("msg1", "商品货号已存在！");
        }
        if (business == null) {
            modelAndView.addObject("msg2", "店铺不存在");
        }
        modelAndView.addObject("inputGoodInfo", goodsInfomation);
        if (Goods == null && business != null) {
            //插入商品
            modelAndView.addObject("msg1", null);
            modelAndView.addObject("msg2", null);
            goodsInfomationService.addGoods(goodsInfomation);
            modelAndView.setViewName("redirect:/allGoodsInfo");
        } else {
            modelAndView.setViewName("backstage/product-add");
        }
        return modelAndView;
    }
}
